package com.ruoyi.common.enums;

/**
 * @Description http状态码枚举所有状态码注解
 * @Author GSX
 * @Date Created in 2022/5/5
 */
public enum HttpStatusEnum {
    /**
     * http状态码枚举所有状态码注解
     */
    NETWORK_EXCEPTION(10000, "NETWORK_EXCEPTION", "网络连接异常!"),
    ADD_EXCEPTION(10001, "ADD_EXCEPTION", "添加异常!"),
    EDIT_EXCEPTION(10002, "EDIT_EXCEPTION", "修改异常!"),
    EXCEPTION_DELETION(10003, "EXCEPTION_DELETION", "删除异常!"),
    QUERY_EXCEPTION(10004, "QUERY_EXCEPTION", "查询异常!"),
    OPERATION_FAILED(10005, "OPERATION_FAILED", "操作失败!");



    private final int code;
    private final String reasonPhraseUS;
    private final String reasonPhraseCN;

    private static final int
            INFORMATIONAL = 1,
            SUCCESSFUL = 2,
            REDIRECTION = 3,
            CLIENT_ERROR = 4,
            SERVER_ERROR = 5;

    HttpStatusEnum(int code, String reasonPhraseUS, String reasonPhraseCN) {
        this.code = code;
        this.reasonPhraseUS = reasonPhraseUS;
        this.reasonPhraseCN = reasonPhraseCN;
    }

    public int code() {
        return code;
    }

    public String reasonPhraseUS() {
        return reasonPhraseUS;
    }

    public String reasonPhraseCN() {
        return reasonPhraseCN;
    }

    public static HttpStatusEnum valueOf(int code) {
        for (HttpStatusEnum httpStatus : values()) {
            if (httpStatus.code() == code) {
                return httpStatus;
            }
        }
        throw new IllegalArgumentException("No matching constant for [" + code + "]");
    }

    public boolean is1xxInformational() {
        return type() == INFORMATIONAL;
    }

    public boolean is2xxSuccessful() {
        return type() == SUCCESSFUL;
    }

    public boolean is3xxRedirection() {
        return type() == REDIRECTION;
    }

    public boolean is4xxClientError() {
        return type() == CLIENT_ERROR;
    }

    public boolean is5xxServerError() {
        return type() == SERVER_ERROR;
    }

    private int type() {
        return (int) code / 100;
    }
}